package main

import (
	"fmt"
)

//0ms 100%, 2MB 12%
func letterCombinations(digits string) []string {
	res := []string{}
	nums := len(digits)
	switch nums {
	case 0:
		return res
	case 1:
		res = append(res, group(digits[0])...)
	case 2:
		for _, v1 := range group(digits[0]) {
			for _, v2 := range group(digits[1]) {
				res = append(res, v1+v2)
			}
		}
	case 3:
		for _, v1 := range group(digits[0]) {
			for _, v2 := range group(digits[1]) {
				for _, v3 := range group(digits[2]) {
					res = append(res, v1+v2+v3)
				}
			}
		}
	case 4:
		for _, v1 := range group(digits[0]) {
			for _, v2 := range group(digits[1]) {
				for _, v3 := range group(digits[2]) {
					for _, v4 := range group(digits[3]) {
						res = append(res, v1+v2+v3+v4)
					}
				}
			}
		}
	}
	return res
}

func group(s byte) []string {
	switch s {
	case '7':
		return []string{"p", "q", "r", "s"}
	case '8':
		return []string{"t", "u", "v"}
	case '9':
		return []string{"w", "x", "y", "z"}
	}
	d := 3 * (s - '2')
	return []string{string('a' + d), string('b' + d), string('c' + d)}
}

func main() {
	fmt.Println(group('9'))
	fmt.Println(group('4'))
	fmt.Println(letterCombinations("5"))
	fmt.Println(letterCombinations("234"))
}
